package guoTeng.netReadCard;

import com.invs.Blt;
import com.invs.invsUtil;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class BltTestActivity extends Activity implements OnClickListener
{
	private Blt mBlt = null;
	private EditText cmd = null;
	private EditText para = null;
	private EditText data = null;
	private EditText result = null;
	private EditText resultData = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.blttest);
		
    	cmd = (EditText) findViewById(R.id.cmdEditText);
    	para = (EditText) findViewById(R.id.paraEditText);
    	data = (EditText) findViewById(R.id.dataEditText);
    	result = (EditText) findViewById(R.id.resultEditText);
    	resultData = (EditText) findViewById(R.id.resultDataEditText);
    	
		for (int i=0; i< id.length; i++){
			Button b = (Button) findViewById(id[i]);
	        b.setOnClickListener(this);	
		}
		{
			Button b = (Button) findViewById(R.id.button4);
	        b.setOnClickListener(this);				
		}
		switchBtn(new int[] {R.id.button1}, true);
		
		mBlt = new Blt();
	}

    @Override
    protected void onDestroy() {
    	super.onDestroy();
    }

	@Override
	public void onClick(View v) {
		if(R.id.button1 == v.getId()){
			switchBtn(new int[] {R.id.button1}, true);
			if (mBlt.connectBt(this, getAddr())){
				switchBtn(new int[] {R.id.button1}, false);
			}
		}else if(R.id.button2 == v.getId()){
			/*
			byte[] data = new byte[2];
			data[0] = 0x0a;
			data[1] = 0x0b;
			int i = mBlt.ExtTransCmd(0x12, 0xff, data, data.length);
			*/
			
			byte[] b1 = invsUtil.hexStringToByte(cmd.getText().toString());
			byte[] b2 = invsUtil.hexStringToByte(para.getText().toString());
			byte[] b3 = invsUtil.hexStringToByte(data.getText().toString());
			int i = mBlt.ExtTransCmd(b1[0], b2[0], b3, b3.length);
			
			result.setText("" + i);
			resultData.setText(invsUtil.bytesToHexString(mBlt.mResultData));
		}else if(R.id.button3 == v.getId()){
			mBlt.disconnectBt();
			switchBtn(new int[] {R.id.button1}, true);
		}else if(R.id.button4 == v.getId()){
			mBlt.disconnectBt();
			finish();
		}
	}   

	static int id[] = {R.id.button1, R.id.button2, R.id.button3};
	private void switchBtn(int btn[], boolean open){
		if (open){			
			for (int i=0; i< id.length; i++){
				Button b = (Button) findViewById(id[i]);
		        b.setEnabled(false)	;	
			}
			for (int i=0; i< btn.length; i++){
				Button b = (Button) findViewById(btn[i]);
		        b.setEnabled(true)	;	
			}
		}else{
			for (int i=0; i< id.length; i++){
				Button b = (Button) findViewById(id[i]);
		        b.setEnabled(true)	;	
			}
			for (int i=0; i< btn.length; i++){
				Button b = (Button) findViewById(btn[i]);
		        b.setEnabled(false)	;	
			}			
		}
	}
	
    private String getAddr()
    {
		SharedPreferences sp = getSharedPreferences("BindDevice", Activity.MODE_PRIVATE);
		return sp.getString("Address", "");
	}
    
    
/*
	Blt blt2 = new Blt();
	if (!blt2.connectBt(this, getAddr()))
		return;
	byte[] data = new byte[2];
	data[0] = 0x0a;
	data[1] = 0x0b;
	int i = blt2.ExtTransCmd(0x20, 0x01, data, data.length);
	blt2.disconnectBt();
*/
}
